#include "Radius2DHandler.h"
#include "CmdManager.h"
#include "Operation/OpReplace.h"
#include "SelectHandler3D.h"
#include "SendPointHandlerBase.h"
#include "Ucs.h"
#include "Scene.h"
#include "QtOSGWidget.h"

Radius2DHandler::Radius2DHandler()
{
}

Radius2DHandler::~Radius2DHandler()
{
}

void Radius2DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	auto scene = getScene();
	auto widget3D = getWidget();
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			selector->clearSelection();
			clearInput();
			if (m_arc)
			{
				auto parent = m_arc->getParent();
				widget3D->pauseRenderThread();
				parent->removeChild(m_arc);
				widget3D->resumeRenderThread();
			}
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D2CurveMask,1);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}
	
	if (status() == 1)
	{
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				auto selection = *selections.begin();
				std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
				m_originArc = std::dynamic_pointer_cast<Arc2D>(ent);
				if (m_originArc)
				{
					auto parent = ent->getParent();
					m_ucs = dynamic_cast<UCS*>(parent);
					m_arc = std::static_pointer_cast<Arc2D>(m_originArc->clone());
					widget3D->pauseRenderThread();
					parent->addChild(m_arc);
					widget3D->resumeRenderThread();
					if (sph)
					{
						sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
						enableMouseHandler(MouseHandlerID::PointSender);
						cmdManager()->setPlane((Plane2DKind)m_ucs->getKind());
					}
					showRubberLine(true);
					setRubberLineP1(m_arc->center());
					prompt(2, tr("指定圆或圆弧半径"));
				}
				else
				{
					selector->clearSelection();
					selector->beginSelect(D2CurveMask, 1);
					remark(tr("请选择圆或圆弧"));
				}
			}
		}
		return;
	}

	if (status() == 2)
	{
		osg::Vec3d ucspt;
		if (data.type() == QVariant::Double)
		{
			double radius = data.value<double>();
			if (radius <= 0.0)
			{
				remark(tr("圆的半径必须不小于0"));
			}
			m_arc->set(m_arc->center(), radius, m_arc->startAngle(), m_arc->endAngle());
			drawFinish(scene);
			emit finished();
		}
		else if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			auto center = m_arc->center();
			auto radius = (center - ucspt).length();
			m_arc->set(center, radius,m_arc->startAngle(),m_arc->endAngle());
			if (cs == CS_MOUSEMOVE)
			{
				setRubberLineP2(ucspt);
			}
			else
			{
				drawFinish(scene);
				emit finished();
				return;
			}
		}
		else
		{
			remark(tr("无效的输入"));
		}
		return;
	}	
}

void Radius2DHandler::drawFinish(Scene * scene)
{
	auto undoStack = scene->undoStack();
	std::vector<std::shared_ptr<Ent>> origins;
	origins.push_back(m_originArc);
	std::vector<std::shared_ptr<Ent>> results;
	results.push_back(m_arc);
	undoStack->push(new OpReplace(origins, results));
}




